package com.atguigu.spzx.manager.mapper;
import org.apache.ibatis.annotations.Param;

import com.atguigu.spzx.manager.entity.SysRoleMenu;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * SysRoleMenuMapper
 * </p>
 *
 * @author flyduck
 * @since 2023/11/2
 */
@Mapper
public interface SysRoleMenuMapper extends BaseMapper<SysRoleMenu> {

    /**
     * 批量插入
     * @param list
     * @return
     */
    int insertList(@Param("list")List<SysRoleMenu> list);

    /**
     * 根据角色id查询
     * @param roleId
     * @return
     */
    default List<SysRoleMenu> getRoleMenuListByRoleId(Long roleId) {
        QueryWrapper<SysRoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", roleId);
        return this.selectList(queryWrapper);
    }

    /**
     * 根据角色id删除
     * @param roleId
     */
    default void deleteByRoleId(Long roleId) {
        QueryWrapper<SysRoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", roleId);
        this.delete(queryWrapper);
    }

    /**
     * 批量新增
     * @param roleId
     * @param menuIdList
     */
    default void insertList(Long roleId, List<Long> menuIdList) {
        List<SysRoleMenu> list = new ArrayList<>();
        for (Long menuId : menuIdList) {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setRoleId(roleId);
            sysRoleMenu.setMenuId(menuId);
            list.add(sysRoleMenu);
        }
        this.insertList(list);
    }


}
